<?php

namespace App\Api\Controllers\v1;

use App\Http\Controllers\ApiController;
use App\Http\Response\ApiResponse;
use App\Models\Coupon;

use App\Models\UserCoupon;
use hg\apidoc\annotation as Apidoc;
use Illuminate\Http\Request;

/**
 * 优惠券接口
 * @Apidoc\Group("user")
 * @Apidoc\Sort(1)
 */
class CouponController extends ApiController
{

    /**
     * 优惠券列表-改
     * @Apidoc\Url("/api/v1/coupon/index")
     * @Apidoc\Method("GET")
     * @Apidoc\Query("shop_id", type="int", desc="门店ID")
     * @Apidoc\Query(ref="queryLongLat")
     * @Apidoc\Query(ref="queryPage")
     * @Apidoc\Returned("items",type="array",ref="App\Models\Coupon")
     * @Apidoc\Returned("total",type="int",desc="总条数")
     * @Apidoc\Returned("lastPage",type="int",desc="最大页码")
     */
    public function index(): ApiResponse
    {
        $data = Coupon::getList($this->user_id);
        return $this->response()->data($data);
    }

    /**
     * 领取优惠券
     * @Apidoc\Url("/api/v1/coupon/receive/:id")
     * @Apidoc\Method("POST")
     * @Apidoc\RouteParam("id",type="int",require=true,desc="优惠券ID")
     * @param Request $request
     * @return ApiResponse
     */
    public function receive(Request $request): ApiResponse
    {
        $id = $request->route("id");
        $res = UserCoupon::addCoupon($id,$this->user_id);
        if ($res){
           return $this->response()->success("领取成功");
        }else{
            return $this->response()->error("领取失败");
        }
    }

    /**
     * 用户优惠券列表
     * @Apidoc\Url("/api/v1/coupon/list")
     * @Apidoc\Method("GET")
     * @Apidoc\Query("type",type="int", require=true, desc="0=可使用,1=不可使用", default="0")
     * @Apidoc\Query("shop_id",type="int", require=false, desc="门店ID", default="0")
     * @Apidoc\Query(ref="queryPage")
     * @Apidoc\Returned("items",type="array",ref="App\Models\UserCoupon")
     * @Apidoc\Returned("total",type="int",desc="总条数")
     * @Apidoc\Returned("lastPage",type="int",desc="最大页码")
     */
    public function list(): ApiResponse
    {
        $user_id = $this->user_id;
        $data = UserCoupon::getList($user_id);
        return $this->response()->data($data);
    }

    /**
     * 根据经纬度和服务获取的用户可使用的优惠券列表-改去掉经纬度参数,新增门店ID参数
     * @Apidoc\Url("/api/v1/coupon/allows")
     * @Apidoc\Method("GET")
     * @Apidoc\Query("service_id",type="int", require=true, desc="服务ID", default="")
     * @Apidoc\Query("price",type="int", require=true, desc="服务价格", default="")
     * @Apidoc\Query("status",type="int", require=true, desc="状态:0=可用,1=不可用", default="")
     * @Apidoc\Query("longitude",type="string", require=true, desc="经度", default="")
     * @Apidoc\Query("latitude",type="string", require=true, desc="纬度", default="")
     * @Apidoc\Returned("items",type="array",ref="App\Models\UserCoupon")
     */
    public function allows(): ApiResponse
    {
        $user_id = $this->user_id;
        $data['items'] = UserCoupon::allows($user_id);
        return $this->response()->data($data);
    }

}
